import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../dd_models/dd_oauth_model.dart';
import '../dd_module/dd_launch_and_main/pages/dd_login_page.dart';
import '../dd_module/dd_personal_center/controllers/dd_personal_center_controller.dart';
import 'dd_config.dart';
import 'dd_hive_service.dart';
import 'dd_http.dart';
import 'dd_other.dart';

class DDUserService extends GetxService {
  String rongCloudKey = '';

  String googleKey = '';

  String rongCloudToken = '';

  DDOauthModel authModel = DDOauthModel();

  String userServiceAccountId = '';

  final availableCoins = 0.obs;

  Future<void> setAuthModel(DDOauthModel model) async {
    authModel = model;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString(DDKey.cureenUid, authModel.userinfo?.userId ?? "");
  }

  Future<void> logout() async {
    Map<String, dynamic>? result = await securityLogout();
    if (result != null && result['code'] == 0) {
      DDAppInfo.getInstance().removeAuthorizationToken();
      authModel = DDOauthModel();
      rongCloudToken = '';
      userServiceAccountId = '';
      availableCoins.value = 0;
      pushLogin();
    }
  }

  Future<void> logoff() async {
    Map<String, dynamic>? result = await userDeleteAccount();
    if (result != null && result['code'] == 0) {
      DDAppInfo.getInstance().removeAuthorizationToken();
      authModel = DDOauthModel();
      rongCloudToken = '';
      userServiceAccountId = '';
      availableCoins.value = 0;
      HiveService().clearBoxes();
      final prefs = await SharedPreferences.getInstance();
      prefs.clear();
      pushLogin();
    }
  }

  pushLogin() {
    navigator
        ?.popUntil((route) => route.settings.name == DDRoutesConfig.splash);
    navigator?.push(DDCustomRoute(DDLoginPage()));
  }

  Future reloadUserInfo() async {
    final prefs = await SharedPreferences.getInstance();
    String cureenUid = await prefs.getString(DDKey.cureenUid) ?? '';
    Map<String, dynamic>? getUserInfoResult = await getUserInfo(cureenUid);
    if (getUserInfoResult != null && getUserInfoResult['code'] == 0) {
      DDUserInfoModel infoModel =
          DDUserInfoModel.fromJson(getUserInfoResult["data"]);
      authModel.userinfo = infoModel;
      if (Get.isRegistered<DDPersonalCenterController>()) {
        Get.find<DDPersonalCenterController>().authModel.value = authModel;
        Get.find<DDPersonalCenterController>().authModel.refresh();
      }
    }
  }

  Future reloadUserCoins() async {
   await Future.delayed(Duration(milliseconds: 100), () async {
      Map<String, dynamic>? result = await getUserCoins();
      if (result != null && result['code'] == 0) {
        availableCoins.value = result['data']['availableCoins'] ?? 0;
        availableCoins.refresh();
      }
    });
  }
}

class AppService extends GetxService {
  final routeNameObs = ''.obs;
  void routeNameChange(String name) {
    if (routeNameObs.value == name) {
      routeNameObs.refresh();
    } else {
      routeNameObs.value = name;
    }
  }

  @override
  void onClose() {
    routeNameObs.close();
  }
}
